﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
using NUnit.Framework;
using SharePoint.Orm.TestHelpers;
using SPGenerated;
using SPGenerated.ContentTypes;
using SPGenerated.Lists;

namespace SharePoint.Orm {
	[TestFixture]
	public class ISPListItemAwareTest : TestBase {
		private int _item1Id;
		private MySPDataContext _ctx;

		[TestFixtureSetUp]
		public override void TestInitialize() {
			base.TestInitialize();
			SiteHelpers.EnsureSite();
			SiteHelpers.RemoveData();

			_ctx = new MySPDataContext(SiteHelpers.Url);

			SPList list = SiteHelpers.EnsureList2(_ctx.Site);

			SPListItem item1 = list.Items.Add();
			item1[SiteHelpers.List2Field1Name] = "Item1";
			item1.Update();
			_item1Id = item1.ID;
		}

		[TestFixtureTearDown]
		public override void TestCleanup() {
			base.TestCleanup();
			_ctx.Dispose();
		}

		[Test]
		public void TestListItem_ListItem() {
			List<List2> list2Items = _ctx.List2.Cast<List2>().ToList();
			Assert.AreEqual(1, list2Items.Count);
			List2 list2Item = list2Items[0];
			Assert.IsNotNull(list2Item.ListItem);
			Assert.AreEqual(_item1Id, list2Item.ListItem.ID);
		}

		[Test]
		public void TestListItem_ContentType() {
			List<List2Item> list2Items = _ctx.List2.OfType<List2Item>().ToList();
			Assert.AreEqual(1, list2Items.Count);
			List2Item list2Item = list2Items[0];
			Assert.IsNotNull(list2Item.ListItem);
			Assert.AreEqual(_item1Id, list2Item.ListItem.ID);
		}
	}
}
